<?php
/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/9/27
 * Time: 14:29
 */

namespace app\common\model;


use think\Model;

class Answer extends Model
{
    public $page_info;

    public function getAnswerList($condition,$page='',$order='id asc'){
        if ($page) {
            $result = $this->where($condition)->order($order)->paginate($page, false, ['query' => request()->param()]);
            $this->page_info = $result;
            return $result->items();
        } else {
            return db('answer')->where($condition)->order($order)->limit(10)->select();
        }
    }

    /**
     * 传入两个选项一个是答案的id，一个是optiond的id，将答案关联到选项
     * @param $ids
     * @param $option_id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function addOptionId($ids, $option_id, $is_add = 1)
    {
        if (is_string($ids)) $ids = explode(',', trim($ids, ','));
        foreach ($ids as $key => $val) {
            if (!$val) continue;
            $model = self::find($val);
            if (!$model) continue;
            if ($is_add == 1) {
                $model->option_id = $option_id;
            } else if ($is_add == 2) {
                $model->option_id = 0;
            }

            $model->save();
        }
    }

    public static function deleteByIds($ids)
    {
        if (is_string($ids)) $ids = explode(',', $ids);
        foreach ($ids as $key => $val) {
            if (!$val) continue;
            $del = explode(',', trim($val, ','));
            self::destroy($del);
        }
    }

    public function addAnswer($data){
        $result = db('answer')->insertGetId($data);
        return $result;
    }

    public function getOneAnswer($condition){
        $result = db('answer')->where($condition)->find();
        return $result;
    }

    public function updateById($data,$id){
        return db('answer')->where('id',$id)->update($data);
    }

    /**
     * 根据传入id返回所有的答案对象
     * @param $id
     *
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getAnswerById($id)
    {
        if (is_string($id)) $id = explode(',', $id);

        return $answer = self::where('id', 'in', $id)->order('order desc')->select();
    }

    /**
     * 给定id查找不存在这些id的答案列表
     * @param $id
     *
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getAnswerByNotInId($id)
    {
        if (is_string($id)) $id = explode(',', $id);

        return $answer = self::where('id', 'notin', $id)->order('order desc')->select();
    }

    /**
     * 关联方法id
     * @param $id
     * @param $method_id
     *
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function addMethod($id, $method_id)
    {
        $model = self::find($id);
        if (!$model) return false;

        if ($model->option_id == 0) $model->option_id = '';
        $method = explode(',', $model->option_id);
        array_push($method, $method_id);
        $method = trim(implode(',', $method), ',');
        $model->option_id = $method;
        if ($model->save()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据答案获取答案下的处理办法
     * @param $id
     *
     * @return AnswerMethod|bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getMethodList($id)
    {
        $model = self::find($id);
        if (!$model) return false;
        return AnswerMethod::getListByIds($model->method_id);
    }

    public static function deleteMethod($id, $method_id)
    {
        $model = self::find($id);
        if (!$model) return false;

        if ($model->method_id == 0) $model->method_id = '';
        $method = explode(',', $model->method_id);
        foreach ($method as $key => $val) {
            if ($val == $method_id) {
                unset($method[$key]);
            }
        }
        $method = trim(implode(',', $method), ',');
        $model->method_id = $method;
        if ($model->save()) {
            return true;
        } else {
            return false;
        }
    }

    public function option()
    {
        return $this->hasOne('QuestionOption', 'id', 'option_id');
    }

    public static function uniqueAnswerByContent($content, $ids = '')
    {
        $content = self::where('content', '=', $content)->select();
        if (is_string($ids)) $ids = explode(',', trim($ids, ','));

        foreach ($content as $key => $val) {
            if (in_array($val['id'], $ids)) {
                return false;
            }
        }
        if ($content) {
            return true;
        }
        return false;
    }

    public static function uniqueMethodByContent($content, $ids = '')
    {
        $method = self::where('method', '=', $content)->select();
        if (is_string($ids)) $ids = explode(',', trim($ids, ','));
        foreach ($method as $key => $val) {
            if (in_array($val['id'], $ids)) {
                return false;
            }
        }
        if ($method) {
            return true;
        }
        return false;
    }





}
